package com.humanmonth.home.component.page.msg.remote
{
	import com.humanmonth.global.Config;
	import com.humanmonth.home.component.page.msg.MsgAs;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	
	/**
	 * 接收入发送留言
	 * @author 林子龙
	 * 
	 */
	public class Connector
	{

//		public static var link:String="http://localhost:9999/";
		public function Connector()
		{
		}
		
		/**
		 * 发送留言
		 * @param func_回调函数
		 * @param name_姓名
		 * @param contact_联系方式
		 * @param msg_信息
		 * 
		 */
		public function postMsg(name:String,contact:String,msg:String):void{
				CursorManager.setBusyCursor();
				var reqVar:URLVariables=new URLVariables();
				reqVar.name=name;
				reqVar.contact=contact;
				reqVar.content=msg;
				var request:URLRequest = new URLRequest(Config.link+"msg"+'&timestamp='+new Date().getTime());
				request.method=URLRequestMethod.POST;
				request.data=reqVar;
				var loader:URLLoader = new URLLoader();
				loader.addEventListener(Event.COMPLETE,dealPostMsg);
				loader.load(request);
		}
		
		private function dealPostMsg(event:Event):void{
			CursorManager.removeBusyCursor();
			Alert.show("感谢你的留言，留言被审核后将显视。");
		}
		
		/**
		 * 获取留言
		 */
		public function getMsgs():void{
			new URLLoader(new URLRequest(Config.link+"msg?t="+new Date().getTime())).addEventListener(Event.COMPLETE,dealGetMsgs);
		}
		
		/**
		 * 处理获取留言结果
		 */
		private function dealGetMsgs(event:Event):void{
			MsgAs.msgs=new XMLListCollection(XML(URLLoader(event.target).data).msg);
		}

	}
}